<?php

namespace plugin\ycMassage\app\admin\controller;

use app\common\builder\ListBuilder;
use app\common\trait\SettingsTrait;
use GuzzleHttp\Client;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\utils\enum\ReadState;
use plugin\ycMassage\utils\WechatHelper;
use think\Request;

class NoticeController extends BaseController
{
    use SettingsTrait;

    /**
     * 应用ID（null则获取系统配置）
     * @var int|null
     * @author 贵州猿创科技有限公司
     * @email 416716328@qq.com
     */
    protected $saas_appid = null;

    /**
     * 构造函数
     * @return void
     * @author 贵州猿创科技有限公司
     * @copyright 贵州猿创科技有限公司
     * @email 416716328@qq.com
     */
    public function initialize()
    {
        parent::initialize();
        $this->saas_appid = $this->request->saas_appid;
    }
    public function getWxTemplateId(Request $request)
    {
        $saas_appid = $request->saas_appid;
        $access_token = WechatHelper::getAccessToken($saas_appid);
        $url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=" . $access_token;
        $params = [
            'template_id_short' => $request->post('template_id_short'),
            'keyword_name_list' => $request->post('keywords'),
        ];
        $client = new Client([
            'content_type' => 'application/json',
        ]);
        $response = $client->post($url, ['body' => json_encode($params, JSON_UNESCAPED_UNICODE)]);
        $data = json_decode($response->getBody()->getContents(), true);
        if (empty($data['template_id'])) {
            throw new \Exception("[{$data['errcode']}]" . $data['errmsg']);
        }
        return $this->successRes(['template_id' => $data['template_id']]);
    }
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->addActionOptions('操作', [
            'width' => 160
        ]);
        $builder->addScreen('content', 'input', '内容', [
            'placeholder' => '内容搜索'
        ]);
        $builder->addScreen('state', 'select', '状态', [
            'options' => ReadState::getOptions()
        ]);
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addRightButton('state', '阅读', [
            'api'    => $this->pluginPrefix . '/admin/Notice/updateState',
            'path' => '/Notice/updateState',
            'type'      => 'modal',
            'params' => [
                'field' => 'state',
                'where' => '=',
                'value' => ReadState::UNREAD['value']
            ]
        ], [], [
            'type' => 'danger',
            'link' => true
        ]);
        $builder->addRightButton('del', '删除', [
            'type'   => 'confirm',
            'api'    => $this->pluginPrefix . '/admin/Notice/delete',
            'method' => 'delete'
        ], [
            'title'   => '温馨提示',
            'content' => '是否确认删除该数据',
        ], [
            'type' => 'error',
            'link' => true
        ]);
        $builder->addColumn('id', '序号', [
            'width' => 90
        ])
            ->addColumn('content', '消息内容')
            ->addColumn('create_time', '推送时间', [
                'width' => 100
            ])
            ->addColumnEle('state', '状态', [
                'width' => 100,
                'params' => [
                    'type' => 'tags',
                    'options' => ReadState::dictOptions(),
                    'style' => ReadState::dictOptions('style')
                ],
            ]);
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        return $this->successRes([]);
    }
}
